﻿
namespace Project1
{
	/// <summary>
	/// An implementation of an Iterative Deepening Depth-First-Search algorithm.
	/// </summary>
	public class IterativeDeepeningDepthFirstSearch : DepthFirstSearch
	{
		/// <summary>
		/// Gets or sets the maximum depth.
		/// </summary>
		/// <value>
		/// The maximum depth.
		/// </value>
		public int MaximumDepth { get; set; }

		/// <summary>
		/// Initializes a new instance of the <see cref="IterativeDeepeningDepthFirstSearch"/> class.
		/// </summary>
		/// <param name="maxDepth">The maximum depth.</param>
		public IterativeDeepeningDepthFirstSearch(int maxDepth)
		{
			this.MaximumDepth = maxDepth;
		}

		/// <summary>
		/// Performs an iterative deepening depth first search
		/// </summary>
		/// <param name="problem">The search problem</param>
		/// <returns>The goal state if a solution exists, otherwise <c>null</c> or cutoff.</returns>
		protected override State PerformSearch(IProblem problem)
		{
			//
			// Start at level 0 and then increment the level by one until we either find a solution, reach
			// the maximum depth limit or have explored the entire state space.
			//
			for (int depth = 0; depth <= MaximumDepth && depth < int.MaxValue; ++depth)
			{
				DepthLimitedSearchResult result = DepthLimitedSearch(problem, depth);
				if (!result.CutOff)
				{
					return result.State;
				}
			}
			return null;
		}
	}
}
